<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="125px"
    >
      <el-form-item label="SerialNo" prop="loanSerialNo">
        <el-input
          v-model="queryParams.loanSerialNo"
          placeholder="请输入借款流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CustNo" prop="custNo">
        <el-input
          v-model="queryParams.custNo"
          placeholder="请输入客户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="产品ID" prop="fkProductId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.fkProductId"-->
      <!--          placeholder="请输入产品ID"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="ProductCode" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ProductName" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PeriodNum" prop="periodNum">
        <el-input
          v-model="queryParams.periodNum"
          placeholder="请输入产品期数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="单位" prop="unit">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.unit"-->
      <!--          placeholder="请输入单位"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="利(息)率" prop="interestRate">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.interestRate"-->
      <!--          placeholder="请输入利(息)率"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="趸交" prop="isSinglePayment">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isSinglePayment"-->
      <!--          placeholder="请输入趸交"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="AccountNumber" prop="accountNumber">
        <el-input
          v-model="queryParams.accountNumber"
          placeholder="请输入银行卡"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="借款期数" prop="tenor">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.tenor"-->
      <!--          placeholder="请输入借款期数"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="RepayDate" prop="repayDate">
        <el-input
          v-model="queryParams.repayDate"
          placeholder="请输入还款日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ContractNo" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PayTransSerialNo" prop="payTransSerialNo">
        <el-input
          v-model="queryParams.payTransSerialNo"
          placeholder="请输入放款流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleRepay"
          v-hasPermi="['business:LoanOrder:edit']"
        >触发放款
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LoanOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" width="100" prop="id"/>
      <el-table-column label="SerialNo" align="center" width="250" prop="loanSerialNo"/>
      <el-table-column label="CustNo" align="center" width="200"  prop="custNo"/>
      <el-table-column label="ProductId" align="center" width="100" prop="fkProductId"/>
      <el-table-column label="ProductCode" align="center" width="150" prop="productCode"/>
      <el-table-column label="ProductName" align="center" width="120" prop="productName"/>
      <el-table-column label="PeriodNum" align="center" width="100" prop="periodNum"/>
      <el-table-column label="Unit" align="center" width="80" prop="unit"/>
      <el-table-column label="InterestRate" align="center" width="110" prop="interestRate"/>
      <el-table-column label="ServiceFee" align="center" width="110" prop="serviceFee"/>
      <el-table-column label="IsSinglePayment" align="center" width="130" prop="isSinglePayment"/>
      <el-table-column label="MinQuota" align="center" width="100" prop="minQuota"/>
      <el-table-column label="MaxQuota" align="center" width="100" prop="maxQuota"/>
      <el-table-column label="DateLateFee" align="center" width="110" prop="dateLateFee"/>
      <el-table-column label="LateFeeGracePeriod" align="center" width="150" prop="lateFeeGracePeriod"/>
      <el-table-column label="LateFeeMaxDays" align="center" width="130" prop="lateFeeMaxDays"/>
      <el-table-column label="LoanAmt" align="center" width="120" prop="loanAmt"/>
      <el-table-column label="AccountNumber" align="center" width="130" prop="accountNumber"/>
      <el-table-column label="Tenor" align="center" width="80" prop="tenor"/>
      <el-table-column label="LoanPurpose" align="center" width="130" prop="loanPurpose" :formatter="loanPurposeFormat"/>
      <el-table-column label="LoanPurposeOther" align="center" width="150" prop="loanPurposeOther"/>
      <el-table-column label="InterestAmt" align="center" width="130" prop="interestAmt"/>
      <el-table-column label="TotalAmt" align="center" width="110" prop="totalAmt"/>
      <el-table-column label="RepayDate" align="center" width="120" prop="repayDate"/>
      <el-table-column label="LoanStatus" align="center" width="150" prop="loanStatus" :formatter="loanStatusFormat"/>
      <el-table-column label="ContractNo" align="center" width="230" prop="contractNo"/>
      <el-table-column label="PayTransSerialNo" align="center" width="280" prop="payTransSerialNo"/>
      <el-table-column label="State" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="CreateTime" align="center" prop="createTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="UpdateTime" align="center" prop="updateTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="Remark" align="center" width="160" prop="remark"/>
      <el-table-column label="Operation" align="center" width="140" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRepay(scope.row)"
            v-hasPermi="['business:LoanOrder:edit']"
          >触发放款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {
  listLoanOrder,
  goRepay
} from "@/api/business/LoanOrderRePay";

export default {
  name: "LoanOrder",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组对象
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 借款申请表格数据
      LoanOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 贷款用途字典
      loanPurposeOptions: [],
      // 借款状态字典
      loanStatusOptions: [],
      // 状态字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        loanSerialNo: null,
        custNo: null,
        fkProductId: null,
        productCode: null,
        productName: null,
        periodNum: null,
        unit: null,
        interestRate: null,
        isSinglePayment: null,
        accountNumber: null,
        tenor: null,
        repayDate: null,
        loanStatus: 6,
        contractNo: null,
        payTransSerialNo: null,
        state: null,
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
    this.getDicts("loan_purpose").then(response => {
      this.loanPurposeOptions = response.data;
    });
    this.getDicts("state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询借款申请列表 */
    getList() {
      this.loading = true;
      listLoanOrder(this.queryParams).then(response => {
        this.loading = false;
        this.LoanOrderList = response.rows;
        this.total = response.total;
      });
    },
    // 贷款用途字典翻译
    loanPurposeFormat(row, column) {
      return this.selectDictLabel(this.loanPurposeOptions, row.loanPurpose);
    },
    // 借款状态字典翻译
    loanStatusFormat(row, column) {
      return this.selectDictLabel(this.loanStatusOptions, row.loanStatus);
    },
    // 状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        loanSerialNo: null,
        custNo: null,
        fkProductId: null,
        productCode: null,
        productName: null,
        periodNum: null,
        unit: null,
        interestRate: null,
        serviceFee: null,
        isSinglePayment: null,
        minQuota: null,
        maxQuota: null,
        dateLateFee: null,
        lateFeeGracePeriod: null,
        lateFeeMaxDays: null,
        loanAmt: null,
        accountNumber: null,
        tenor: null,
        loanPurpose: null,
        loanPurposeOther: null,
        interestAmt: null,
        totalAmt: null,
        repayDate: null,
        loanStatus: null,
        contractNo: null,
        payTransSerialNo: null,
        state: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;

      this.rows = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加借款申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLoanOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改借款申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLoanOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLoanOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleRepay(row) {
      const ids = row.id || this.ids;
      this.$confirm('确认为选中的订单进行重新放款？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return goRepay(ids);
      }).then((res) => {
        this.getList();
        this.$msgbox(res.msg);
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有借款申请数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportLoanOrder(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    //手动触发
    handelTriggerAgain() {
      var rows = this.rows;
      var flag = rows.length == 0 ? 0 : 1;
      rows.forEach((item) => {
        if (item.loanStatus != "9") {
          flag = 2;
        }
      });
      if (flag != 1) {
        this.$message({
          message:
            flag == 0
              ? "请至少选择一条 Under Review 的记录"
              : "已选择的记录中包含非 Under Review 的状态",
          type: "warning",
        });
        return;
      }
      var loanNos = rows.map((item) => item.loanSerialNo);
      this.loading = true;
      manual_loan(loanNos)
        .then((res) => {
          this.loading = false;
          this.getList();
          this.$alert(res.data, '操作返回', {
            confirmButtonText: "确定",
            callback: (action) => {
              this.$message({
                type: "success",
                message: `操作成功`,
              });
            },
          });
        })
        .catch(() => {
          this.loading = false;
        });
      console.log("发起请求", rows);
    },
  },
};
</script>
